/*
 * Copyright (c) 2016 EEFUNG Software Co.Ltd. All rights reserved.
 * 版权所有(c)2016湖南蚁坊软件有限公司。保留所有权利。
 */

package com.zidian.leader.common.util;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 提供日期时间相关实用方法
 * Created by Zuo Ji on 2015/11/30.
 */
public final class DatetimeUtils {

    /**
     * 日期时间格式化字符串：yyyy-MM-dd HH:mm:ss
     */
    public static final String DATE_TIME_FORMAT_PATTERN_WITH_SECOND = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期时间格式化字符串：MM月dd日
     */
    public static final String DATE_TIME_FORMAT_PATTERN_WITH_MONTH_DAY = "MM月dd日";

    /**
     * 1 秒钟的毫秒数
     */
    public static final long MILLISECONDS_OF_1_SECOND = 1000L;

    /**
     * 1 分钟的毫秒数
     */
    public static final long MILLISECONDS_OF_1_MINUTE = 1000 * 60L;

    /**
     * 1 小时的毫秒数
     */
    public static final long MILLISECONDS_OF_1_HOUR = 1000 * 60 * 60L;

    /**
     * 12 小时的毫秒数
     */
    public static final long MILLISECONDS_OF_12_HOURS = 1000 * 60 * 60 * 12L;

    /**
     * 24 小时的毫秒数
     */
    public static final long MILLISECONDS_OF_24_HOURS = MILLISECONDS_OF_12_HOURS * 2L;

    /**
     * 三天的毫秒数
     */
    public static final long MILLISECONDS_OF_THREE_DAYS = MILLISECONDS_OF_24_HOURS * 3L;

    /**
     * 将日期格式，进行按照给定的格式格式化
     *
     * @param date 日期
     * @return 格式化之后的字符串
     */
    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT_PATTERN_WITH_SECOND);
        return sdf.format(date);
    }

    /**
     * 将日期格式，进行按照给定的格式格式化
     *
     * @param date 日期
     * @return 格式化之后的字符串
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 格式化日期时间对象到友好形式
     *
     * @param datetime 需要格式化的日期时间
     * @return 格式化之后的字符串
     */
    public static String formatToFriendlyForm(Date datetime) {
        if (datetime == null) {
            return "未知";
        }
        Calendar now = Calendar.getInstance();
        Calendar target = Calendar.getInstance();
        target.setTime(datetime);

        int nowYear = now.get(Calendar.YEAR);
        int nowMonth = now.get(Calendar.MONTH);
        int nowDay = now.get(Calendar.DAY_OF_MONTH);

        int targetYear = target.get(Calendar.YEAR);
        int targetMonth = target.get(Calendar.MONTH);
        int targetDay = target.get(Calendar.DAY_OF_MONTH);

        int moveut = (int) ((now.getTime().getTime() - datetime.getTime()) / 1000);

        if (moveut < 3600) { // 小于
            return "今天 " + format(datetime, "HH:mm");
        }
        if (targetYear != nowYear) {
            if (targetMonth == 11 && nowMonth == 0) {
                return format(datetime, "yyyy年MM月dd日 HH:mm");
            }
            return format(datetime, "yyyy年MM月dd日");
        }
        if (targetMonth != nowMonth) {
            return format(datetime, "MM月dd日 HH:mm");
        }
        if (targetDay != nowDay) {
            if (targetDay + 1 == nowDay) {
                return "昨天 " + format(datetime, "HH:mm");
            }
            return format(datetime, "MM月dd日 HH:mm");
        }

        return "今天 " + format(datetime, "HH:mm");
    }

    /**
     * 转换毫秒数到友好的形式
     *
     * @param millisecond 时间 毫秒
     * @return 格式化之后的字符串
     */
    public static String convertMillisecondToFriendlyForm(long millisecond) {
        NumberFormat numberFormat = NumberFormat.getIntegerInstance();
        numberFormat.setMinimumIntegerDigits(1);
        numberFormat.setMinimumFractionDigits(0);
        numberFormat.setMaximumFractionDigits(1);

        double time;
        String suffix;

        if (millisecond >= MILLISECONDS_OF_24_HOURS) {
            time = (double) millisecond / MILLISECONDS_OF_24_HOURS;
            suffix = "天";
        } else if (millisecond >= MILLISECONDS_OF_1_HOUR) {
            time = (double) millisecond / MILLISECONDS_OF_1_HOUR;
            suffix = "小时";
        } else if (millisecond >= MILLISECONDS_OF_1_MINUTE) {
            time = (double) millisecond / MILLISECONDS_OF_1_MINUTE;
            suffix = "分钟";
        } else if (millisecond >= MILLISECONDS_OF_1_SECOND) {
            time = (double) millisecond / MILLISECONDS_OF_1_SECOND;
            suffix = "秒钟";
        } else {
            time = millisecond;
            suffix = "毫秒";
        }

        return numberFormat.format(time) + suffix;
    }
}